package com.chinawlx.wlxteacher.ui.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chinawlx.wlxteacher.R;
import com.chinawlx.wlxteacher.network.WLXHttpRxHelper;
import com.chinawlx.wlxteacher.network.bean.WLXAboutFutrueStarBean;
import com.chinawlx.wlxteacher.utils.WLXDateUtil;
import com.chinawlx.wlxteacher.utils.WLXGreenDaoUtil;
import com.chinawlx.wlxteacher.utils.WLXNetUtil;
import com.chinawlx.wlxteacher.widget.WLXCustomToast;
import com.chinawlx.wlxteacher.wlx_config;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import rx.Subscriber;


/**
 * Created by wlx on 16/9/5.
 */
public class WLXAboutFutrueStarActivity extends WLXBaseActivity {
    @BindView(R.id.tv_about_title)
    TextView mTvAboutTitle;
    @BindView(R.id.tv_about_content_profile)
    TextView mTvAboutContentProfile;
    @BindView(R.id.tv_about_content_why)
    TextView mTvAboutContentWhy;
    @BindView(R.id.iv_back_about)
    ImageView backBtn;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about_futurestar);
        ButterKnife.bind(this);
        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        if (!WLXNetUtil.netIsAvailable()){
            loadDataFromDb();
            return;
        }
        WLXHttpRxHelper.getInstance().aboutFutureStar(new Subscriber<WLXAboutFutrueStarBean>() {
            @Override
            public void onCompleted() {
                loadDataFromDb();
            }

            @Override
            public void onError(Throwable e) {
                onCompleted();
            }

            @Override
            public void onNext(WLXAboutFutrueStarBean aboutFutrueStarBean) {
                if (aboutFutrueStarBean.getCode() == 0) {
                    String key = aboutFutrueStarBean.getData().getKey();
                    long last_modified_date = WLXDateUtil.getStringToDate(aboutFutrueStarBean
                            .getData().getLast_modified_date());
                    int version = aboutFutrueStarBean.getData().getVersion();
                    WLXAboutFutrueStarBean.DataBean.ValueBean value = aboutFutrueStarBean.getData
                            ().getValue();

                    List<wlx_config> list = WLXGreenDaoUtil.getConfigDao().queryBuilder().list();
                    if (list != null && list.size() > 0) {
                        if (last_modified_date > list.get(0).getLast_modified_date()) {
                            //更新数据库
                            wlx_config config = list.get(0);
                            config.setKey(key);
                            config.setValue(new Gson().toJson(value));
                            config.setVersion(version);
                            config.setLast_modified_date(last_modified_date);
                            WLXGreenDaoUtil.getConfigDao().update(config);
                        }
                    } else {
                        //插入数据库
                        wlx_config wlxConfig = new wlx_config(key, new Gson().toJson(value),
                                version, last_modified_date);
                        WLXGreenDaoUtil.getConfigDao().insert(wlxConfig);
                    }
                } else {
                    WLXCustomToast.show(aboutFutrueStarBean.getMsg());
                }
            }

        });
    }

    private void loadDataFromDb() {
        //从数据库中读取数据显示
        List<wlx_config> list = WLXGreenDaoUtil.getConfigDao().queryBuilder().list();
        if (list != null && list.size() > 0) {
            WLXAboutFutrueStarBean.DataBean.ValueBean value = new Gson().fromJson(list
                    .get(0).getValue(), new
                    TypeToken<WLXAboutFutrueStarBean.DataBean.ValueBean>() {
                    }.getType());
            //显示
            mTvAboutTitle.setText(value.getTitle());
            String[] content = value.getContent().split("\\u0024");
            mTvAboutContentProfile.setText(content[0]);
            mTvAboutContentWhy.setText(content[1]);
        }
    }


}
